<template>
  <div class="addBrand-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/employee' }">返回</el-breadcrumb-item>
      <el-breadcrumb-item>{{$route.query.id ? '修改员工' : '新增员工'}}</el-breadcrumb-item>
    </el-breadcrumb>
    <hr/>
    <div class="container">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="ruleForm.sex" label="1">男</el-radio>
          <el-radio v-model="ruleForm.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="ruleForm.idNumber"></el-input>
        </el-form-item>
        <div class="subBox">
          <el-button type="primary" @click="submitForm('ruleForm',false)">保存</el-button>
          <el-button
            v-if="this.optType === 'add'"
            type="primary"
            @click="submitForm('ruleForm',true)">保存并继续添加员工
          </el-button>
          <el-button @click="() => this.$router.push('/employee')">返回</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { addEmployee, queryEmployeeById, updateEmployee} from "@/api/employee";
export default {
  data() {
    return {
      optType: '',//操作类型，新增or修改
      ruleForm: {
        name: '',
        username: '',
        phone: '',
        sex: '1',
        idNumber: ''
      },
      rules: {
        name: [
          { required: true, message:'请输入员工姓名', trigger: 'blur' },
        ],
        username: [
          { required: true, message: '请输入员工账号', trigger: 'blur' },
          { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, trigger: 'blur', validator: (rule, value, callback) =>{
              if (value === '' || (!/^1[3-8]\d{9}$/.test(value))) {
                callback(new Error('请输入正确的手机号'));
              }
              else {
                callback();
              }
            }
          }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        idNumber: [
          { required: true, trigger: 'blur', validator: (rule, value, callback) => {
              // 15位身份证号，验证以前的身份证号码
              if (value === '' || !(/(^\d{15}$)|(^\d{18}$)|(^\d{17}[Xx]$)/.test(value))) {
                callback(new Error(`身份证号【${value}】格式不正确，请输入正确的身份证号码`));
              }
              else {
                callback();
              }
            }
          }
        ]
      }
    }
  },

  methods: {
    submitForm(formName: string, isContinue: boolean) {
      const _this = this;
      //表单校验
      _this.$refs[formName].validate((valid: boolean) => {
        if (valid) {
          //发送ajax请求,将数据传递到后端
          if (_this.optType === 'add') {
            addEmployee(_this.ruleForm).then(res => {
              if (res.data.code === 1) {
                if (isContinue) {
                  _this.ruleForm = {
                    name: '',
                    username: '',
                    phone: '',
                    sex: '1',
                    idNumber: ''
                  };
                }
                else {
                  _this.$router.push('/employee');
                }
                _this.$message.success('员工添加成功！');
              }
              else {
                _this.$message.error(res.data.msg);
              }
            });
          }
          else {
            updateEmployee(_this.ruleForm).then(res => {
              if (res.data.code === 1) {
                _this.$message.success('员工修改成功！');
                _this.$router.push('/employee');
              }
            });
          }
        }
      });
    }
  },

  created() {
    const _this = this;
    //获取路由参数，判断是修改还是新增
    _this.optType = _this.$route.query.id ? 'update' : 'add';

    if (_this.optType === 'update') {
      //修改
      queryEmployeeById(_this.$route.query.id).then(res => {
        if(res.data.code === 1) {
          _this.ruleForm = res.data.data;
        }
      });
    }
  },
}
</script>

<style lang="scss" scoped>
.addBrand {
  &-container {
    margin: 30px;
    .HeadLable {
      background-color: transparent;
      margin-bottom: 0;
      padding-left: 0;
    }
    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 4px;
      // min-height: 500px;
      .subBox {
        padding-top: 30px;
        text-align: center;
        border-top: solid 1px $gray-5;
      }
    }
    .idNumber {
      margin-bottom: 39px;
    }

    .el-form-item {
      margin-bottom: 29px;
      margin-left: 25%;
    }
    .el-input {
      width: 293px;
    }
  }
}
</style>
